define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/usertwo/index' + location.search,
                    add_url: 'user/usertwo/add',
                    edit_url: 'user/usertwo/edit',
                    del_url: 'user/usertwo/del',
                    multi_url: 'user/usertwo/multi',
                    chongzhi_url: 'user/usertwo/chongzhi',
                    // import_url: 'user/usertwo/import',
                    table: 'user_two',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'username',
                            title: __('Username'),
                            operate: 'LIKE',
                            formatter: function (value, row, index) {
                                var str = value + "<br />" + row.invite_code +"<br />";
                                if(row.avatar){
                                    str = value + "<br />" + row.invite_code +"<br />"+"<span style='text-align: center; vertical-align: middle; '><a href='javascript:;'><img class='img-sm img-center' src="+row.avatar+">" + "</a></span>";;
                                }

                                return str;
                            }
                        },
                        // {
                        //     field: 'invite_code',
                        //     title: __('Invite_code'),
                        //     operate: 'LIKE',
                        //     table: table,
                        //     class: 'autocontent',
                        //     formatter: Table.api.formatter.content
                        // },
                        {
                            field: 'parentuser.mobile',
                            title: __('上级用户'),
                            operate: 'LIKE',
                            // formatter: function (value, row, index) {
                            //     if(!value){
                            //         value = '无';
                            //     }
                            //     if(!row.parentuser.mobile){
                            //         row.parentuser.mobile = '';
                            //     }
                            //     return value + "<br />" + "<span style='color:#708eea;'>" + row.parentuser.mobile + "</span>";
                            // }
                        },

                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        // {field: 'pa_rate', title: __('帕点'),  formatter: Table.api.formatter.status,
                        //     searchList: {0: __('0'), 10: __('10'),15:__('15')}},
                        {
                            field: 'status',
                            title: __('Status'),
                            formatter: Table.api.formatter.status,
                            searchList: {normal: __('Normal'), hidden: __('禁用')}
                        },
                        {
                            field: 'is_old',
                            title: __('是否老用户'),
                            formatter: Table.api.formatter.toggle,
                            searchList: {"1": __('是'), "0": __('否')}
                        },
                        {
                            field: 'is_partner',
                            title: __('合伙人'),
                            formatter: Table.api.formatter.toggle,
                            searchList: {"1": __('是'), "0": __('否')}
                        },
                        {
                            field: 'is_withdraw',
                            title: __('禁止提现'),
                            formatter: Table.api.formatter.toggle,
                            searchList: {"1": __('是'), "0": __('否')}
                        },
                        // {
                        //     field: 'is_firepower',
                        //     title: __('启用火力值'),
                        //     formatter: Table.api.formatter.toggle,
                        //     searchList: {"1": __('是'), "0": __('否')}
                        // },
                        {
                            field: 'is_frozen',
                            title: __('冻结'),
                            formatter: Table.api.formatter.toggle,
                            searchList: {"1": __('是'), "0": __('否')}
                        },
                        {
                            field: 'is_lock',
                            title: __('锁定'),
                            formatter: Table.api.formatter.toggle,
                            searchList: {"1": __('是'), "0": __('否')}
                        },
                        // {
                        //     field: 'avatar',
                        //     title: __('Avatar'),
                        //     operate: 'LIKE',
                        //     events: Table.api.events.image,
                        //     formatter: Table.api.formatter.image
                        // },
                        {field: 'level', title: __('Level')},
                        {field: 'daquyeji', title: __('大区'), operate: false},
                        {field: 'xiaoquyeji', title: __('小区'), operate: false},
                        {field: 'money', title: __('Money'), operate: 'BETWEEN'},
                        {field: 'score', title: __('Score'),operate: 'BETWEEN'},
                        {field: 'principal', title: __('本金'),operate: 'BETWEEN'},
                        {field: 'golden', title: __('Golden'), operate: 'BETWEEN'},
                        {field: 'silver', title: __('Silver'), operate: 'BETWEEN'},
                        // {field: 'gold', title: __('Gold'), operate: 'BETWEEN'},
                        {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'team_buy_money', title: __('累计业绩'), operate: 'BETWEEN'},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    title: __('充值'),
                                    text: '充值',
                                    // classname: 'btn btn-xs btn-warning btn-addtabs',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-rmb',
                                    url: 'user/usertwo/chongzhi?id={id}'
                                },
                                {
                                    title: __('查看团队'),
                                    text: '查看团队',
                                    // classname: 'btn btn-xs btn-warning btn-addtabs',
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-folder-o',
                                    url: 'user/Children/index?member_id={id}'
                                },
                                {
                                    title: __('禁用团队'),
                                    text: '禁用团队',
                                    classname: 'btn btn-xs btn-blue btn-ajax',
                                    icon: 'fa fa-magic',
                                    confirm: '确认要禁用团队登录吗？',
                                    url: 'user/usertwo/jinyong?status=hidden',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.team_status == 1) {
                                            return true;
                                        } else {
                                            return false;
                                        }
                                    },
                                    success: function (data, ret) {
                                        $('.btn-refresh').trigger('click');
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    title: __('启用团队'),
                                    text: '启用团队',
                                    classname: 'btn btn-xs btn-blue btn-ajax',
                                    icon: 'fa fa-magic',
                                    confirm: '确认要启用团队登录吗？',
                                    url: 'user/usertwo/jinyong?status=normal',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.team_status == 2) {
                                            return true;
                                        } else {
                                            return false;
                                        }
                                    },
                                    success: function (data, ret) {
                                        $('.btn-refresh').trigger('click');
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    title: __('禁止团队提现'),
                                    text: '禁止团队提现',
                                    classname: 'btn btn-xs btn-blue btn-ajax',
                                    icon: 'fa fa-magic',
                                    confirm: '确认要禁用团队提现吗？',
                                    url: 'user/usertwo/jinyong?is_withdraw=1',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.team_withdraw == 1) {
                                            return true;
                                        } else {
                                            return false;
                                        }
                                    },
                                    success: function (data, ret) {
                                        $('.btn-refresh').trigger('click');
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    title: __('启用团队提现'),
                                    text: '启用团队提现',
                                    classname: 'btn btn-xs btn-blue btn-ajax',
                                    icon: 'fa fa-magic',
                                    confirm: '确认要启用团队提现吗？',
                                    url: 'user/usertwo/jinyong?is_withdraw=0',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.team_withdraw == 2) {
                                            return true;
                                        } else {
                                            return false;
                                        }
                                    },
                                    success: function (data, ret) {
                                        $('.btn-refresh').trigger('click');
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    title: __('火力值'),
                                    text: '启用火力值',
                                    classname: 'btn btn-xs btn-blue btn-ajax',
                                    icon: 'fa fa-magic',
                                    confirm: '确认要启用火力值吗？',
                                    url: 'user/usertwo/jinyong?is_firepower=1',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.team_firepower == 2) {
                                            return true;
                                        } else {
                                            return false;
                                        }
                                    },
                                    success: function (data, ret) {
                                        $('.btn-refresh').trigger('click');
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    title: __('禁止火力值'),
                                    text: '禁用火力值',
                                    classname: 'btn btn-xs btn-blue btn-ajax',
                                    icon: 'fa fa-magic',
                                    confirm: '确认要禁用团队禁用火力值吗？',
                                    url: 'user/usertwo/jinyong?is_firepower=0',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.team_firepower == 1) {
                                            return true;
                                        } else {
                                            return false;
                                        }
                                    },
                                    success: function (data, ret) {
                                        $('.btn-refresh').trigger('click');
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        chongzhi: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
